/*
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#include <string.h>

#include "gelogger.h"

void        gelogger_set( t_gelogger* gelogger )
{
    
    //set gelogger structure here    
    gelogger->logfile = get_default_logfile();
    gelogger->rep_absence_time = 1200;
    gelogger->nologtime = 1200;

    be_set_database_name(gelogger->logfile);
    
    report( gelogger, time(NULL), "[WLSTART] starting gelogger" );

}

int report( t_gelogger* gelogger, time_t _time, char* _message )
{
        be_report( _time, _message );
/*    
		FILE* log = fopen( gelogger->logfile, "a" );

    	char* strtime = ctime( &_time );

		strtime[ strlen( strtime ) - 1 ] = '\0';
	
		if( NULL == log )
		{
			return FALSE;
		}

		fprintf( log, "%s: %s\n", strtime , _message );

		fclose( log );
		
		gelogger->nologcounter = 0;
*/		
		return TRUE;
}

int debug( int _level, t_gelogger gelogger, char* _message )
{
		if( gelogger.debug < _level )
		{
			return 0;
		}

		char* strtime = "ctime( &time )";

		printf( "%s: %s\n", strtime , _message );

		return TRUE;
}

char* get_default_logfile()
{
	const gchar* homedir = g_get_home_dir();

	gchar* out = g_strndup( homedir, strlen( homedir ) + strlen( DEF_LOGFILE ) );

	strcat( out, DEF_LOGFILE );

	return out;	
}


int detect_idle( t_gelogger* gelogger )
{
	t_idler new;
	int  idle = 0;
	
	GdkScreen* screen;

	memcpy( &new, &(gelogger->idler), sizeof( t_idler ) );

	gdk_display_get_pointer( gdk_display_get_default(), &screen, &new.x, &new.y, &new.mask );
	
	if( abs( new.x - gelogger->idler.x ) < 10 || abs( new.y - gelogger->idler.y ) < 10 )
	{
		idle = 1;
	}

	memcpy( &(gelogger->idler), &new, sizeof( t_idler ) );

	return idle;
	
}

gboolean gelogger_check_absence( t_gelogger* gelogger )
{
	char msg[1000];

	sprintf( msg, "[debug] check absence [rep_absence_time=%d],[nologcounter=%d],[nologtime=%d]", gelogger->rep_absence_time, gelogger->nologcounter, gelogger->nologtime );

//	debug( GLDBG, *gelogger, msg );


	/* Query xscreensaver, not very reliable */
#if 0
	static XScreenSaverInfo *mit_info = NULL;
	int event_base, error_base;
	time_t	idle = 0;

	if (XScreenSaverQueryExtension(GDK_DISPLAY(), &event_base, &error_base)) 
	{
			debug( 4, gelogger, "[debug] get xscreensaver's info" );
			if (mit_info == NULL) {
					mit_info = XScreenSaverAllocInfo();
			}
			XScreenSaverQueryInfo(GDK_DISPLAY(), GDK_ROOT_WINDOW(), mit_info);
			idle = (mit_info->idle) / 1000;
	}
#endif

	int idle = detect_idle( gelogger );

	sprintf( msg, "[debug] check absence [idle=%d] [absence_start=%d] [rep_absence_time=%d]", 
		idle, gelogger->absence_start, gelogger->rep_absence_time );

//	debug( GLTRC, gelogger, msg );

	if( !idle ) 
	{
		gelogger->absence_start = time( NULL );
	}
	

	if( idle && time( NULL ) - gelogger->absence_start > gelogger->rep_absence_time )
	{
//		debug( GLDBG, gelogger, "[debug] away detected" );

		if( 0 == gelogger->is_away ) 
		{
			/* abesence detected */
			gelogger->is_away = 1;
			/* report absence start */
			report( gelogger, gelogger->absence_start, "[WLSTOP] stop working detected" );
		}

		gelogger->work_start = time( NULL );
	}
	else
	{
		if( 1 == gelogger->is_away ) 
		{
			/* we are back */

			gelogger->is_away = 0;
/*			wl->absence_start = time( NULL ); */

			/* report it */
			report( gelogger, gelogger->work_start, "[WLSTART] start working detected" );
		}


	}

	gelogger->nologcounter++;
	
	return TRUE;
}


